package com.sinoglobal.dumping.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoglobal.dumping.R;

/**
 * Created by lxy on 2015/10/30.
 */
public class Dumpling_GetDumplingDialog extends Dialog {
    private Context mContext;

    private LayoutInflater mInflater;

    private ImageView imgIcon;

    private TextView tvtitle;

    private TextView tvMonery;

    private TextView tvLeft;

    private TextView tvRight;

    private TextView tvClose;

    private TextView tvFailureSure;

    private TextView tvToast;

    private LinearLayout llSuccess;


    private LinearLayout llFailure;

    private GetDumplingOnSureClick mClick;

    public static final int DUMPLING_FAILURE = 1;
    
    public static final int DUMPLING_ERRO = 2;

    public interface GetDumplingOnSureClick {

        void onSuccessLeftClick();

        void onSuccessRightClick();

        void onSuccessCloseClick();

        void onFailureSureClick();
    }


    public Dumpling_GetDumplingDialog(Context context) {
        super(context, R.style.dumpling_dialog);
        
        mContext = context;
        mInflater = LayoutInflater.from(context);
        setCanceledOnTouchOutside(false);

        View view = mInflater.inflate(R.layout.dumpling_dialog_getdumpling, null);
        setContentView(view);
        llSuccess = (LinearLayout) view.findViewById(R.id.dialog_getdumpling_ll_success);
        llFailure = (LinearLayout) view.findViewById(R.id.dialog_getdumpling_ll_failure);
        imgIcon = (ImageView) view.findViewById(R.id.dialog_getdumpling_img_icon);
        tvtitle = (TextView) view.findViewById(R.id.dialog_getdumpling_tv_title);
        tvMonery = (TextView) view.findViewById(R.id.dialog_getdumpling_tv_monery);
        tvLeft = (TextView) view.findViewById(R.id.dialog_getdumpling_tv_left);
        tvRight = (TextView) view.findViewById(R.id.dialog_getdumpling_tv_right);
        tvClose = (TextView) view.findViewById(R.id.dialog_getdumpling_tv_close);
        tvFailureSure = (TextView) view.findViewById(R.id.dialog_getdumpling_tv_failure_sure);
        tvToast = (TextView) view.findViewById(R.id.dialog_getdumpling_tv_toast_share);
        initClick(null);
    }

    public void initClick(GetDumplingOnSureClick getDumplingOnSureClick) {
        mClick = getDumplingOnSureClick;
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClick!=null){
                    mClick.onSuccessLeftClick();
                    dismiss();
                }
            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClick!=null){
                    mClick.onSuccessRightClick();
                    dismiss();
                }
            }
        });
        tvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClick != null) {
                    mClick.onSuccessCloseClick();
                }
                if (isShowing()) {
                    dismiss();
                }
            }
        });
        tvFailureSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClick != null) {
                    mClick.onFailureSureClick();
                }
                if (isShowing()) {
                    dismiss();
                }
            }
        });

    }

    @Override
    public void show() {
        super.show();
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth(); //设置宽度
        lp.width -= 110;
        getWindow().setAttributes(lp);
    }

    /**
     * 捞取失败调用
     */
    public void showGetDumplingFailure() {
        changeState(DUMPLING_FAILURE, null);
        if(!((FragmentActivity)mContext).isFinishing()){
            show();
        }
    }
    public TextView getTitle(){
        return  tvtitle;
    }
    /**
     * @param state ture:捞取成功；false：捞取失败
     */
    void changeState(int state, String msg) {
        if (state==DUMPLING_FAILURE) {
            imgIcon.setImageResource(R.drawable.dumpling_nothappy);
            tvtitle.setText(mContext.getResources().getString(R.string.dumpling_getdumpling_failure));
            tvToast.setVisibility(View.GONE);
            tvClose.setVisibility(View.GONE);
            llFailure.setVisibility(View.VISIBLE);
            llSuccess.setVisibility(View.GONE);
        } else if(state == DUMPLING_ERRO){
            imgIcon.setImageResource(R.drawable.dumpling_nothappy);
            tvtitle.setText(mContext.getResources().getString(R.string.dumpling_getdumpling_erro));
            tvToast.setVisibility(View.GONE);
            tvClose.setVisibility(View.GONE);
            llFailure.setVisibility(View.VISIBLE);
            llSuccess.setVisibility(View.GONE);
        }else{
            imgIcon.setImageResource(R.drawable.dumpling_happly);
            tvtitle.setText(mContext.getResources().getString(R.string.dumpling_getdumpling_success));
            tvToast.setVisibility(View.VISIBLE);
            tvClose.setVisibility(View.VISIBLE);
            llFailure.setVisibility(View.GONE);
            llSuccess.setVisibility(View.VISIBLE);
        }
        
        if (!TextUtils.isEmpty(msg)) {
            tvMonery.setText(msg);
            tvMonery.setVisibility(View.VISIBLE);
        } else {
            tvMonery.setText("");
            tvMonery.setVisibility(View.GONE);
        }
    }
}
